package web

import (
	"github.com/aphasia51/website/internal/domain"
	"github.com/aphasia51/website/internal/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

type ConsumablesHandler struct {
	svc *service.ConsumablesService
}

func NewConsumablesHandler(svc *service.ConsumablesService) *ConsumablesHandler {
	return &ConsumablesHandler{
		svc: svc,
	}
}

func (c *ConsumablesHandler) RegistryRoutes(server *gin.Engine) {
	ag := server.Group("/consumable")
	ag.POST("/query", c.Query)
	ag.POST("/select", c.Select)
}

func (c *ConsumablesHandler) Query(ctx *gin.Context) {
	type QueryReq struct {
		Name string `json:"consumableName"`
	}
	var req QueryReq
	if err := ctx.Bind(&req); err != nil {
		panic(err)
	}
	Consumables, err := c.svc.Query(ctx, domain.Consumable{
		Name: req.Name,
	})

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, Consumables)
}

func (c *ConsumablesHandler) Select(ctx *gin.Context) {

	consumables, err := c.svc.Select(ctx)

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, consumables)
}
